package com.wanghang.code.common.exception;

import com.wanghang.code.bean.IResultCode;
import org.slf4j.helpers.MessageFormatter;

public class BusinessException extends RuntimeException{

    private final transient IResultCode resultCode;

    public BusinessException(IResultCode errorCode) {
        super(errorCode.getMsg());
        this.resultCode = errorCode;
    }


    public BusinessException(IResultCode errorCode, String message) {
        super(message);
        this.resultCode = errorCode;
    }


    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.resultCode = null;
    }

    public BusinessException(Throwable cause) {
        super(cause);
        this.resultCode = null;
    }

    public BusinessException(String message, Object... args) {
        super(formatMessage(message, args));
        this.resultCode = null;
    }

    private static String formatMessage(String message, Object... args) {
        return MessageFormatter.arrayFormat(message, args).getMessage();
    }
}
